#include "qmemeryadddialog.h"
#include "ui_qmemeryadddialog.h"

#include "../qsoftcore.h"

#include "../../shared/property/qabstractproperty.h"
#include "../../shared/host/qabstracthost.h"
#include "../../shared/qprojectcore.h"
#include "../../shared/qdatamanager.h"
#include "../../shared/qdata.h"

QMemeryAddDialog::QMemeryAddDialog(QAbstractProperty *property, QWidget *parent):
    QBaseDialog(parent),
    ui(new Ui::QMemeryAddDialog)
{
    ui->setupUi(this);

    QAbstractHost* host=property->get_host();

    QString strs;

    QList<QAbstractProperty*> pros=host->get_propertys();

    QAbstractProperty* pro;
    QString str;
    QStringList l;
    while(pros.size()>0)
    {
        pro=pros.takeFirst();
        if(!pro->get_attribute(ATTR_VISIBLE).toBool()
                || !pro->get_attribute(ATTR_EDITABLE).toBool()
                || pro->get_attribute(ATTR_NOT_DATA).toBool())
        {
            continue;
        }
        if(pro->get_children().size()>0)
        {
            pros+=pro->get_children();
            continue;
        }
        str=pro->get_property("type").toString();
        if(str!="Bool"
                && str!="String"
                && str!="ByteArray"
                && str!="Number"
                && str!="Float"
                && str!="Enum")
        {
            continue;
        }
        str=pro->get_name();
        strs=pro->get_attribute("show_name").toString();
        pro=pro->get_parent();
        while(pro!=NULL)
        {
            strs=pro->get_attribute("show_name").toString()+"."+strs;
            pro=pro->get_parent();
        }
        m_name_to_property.insert(strs,str);
        l.append(strs);
    }

    ui->comboBox_2->addItems(l);
    ui->comboBox_2->setCurrentIndex(0);

    l.clear();
    foreach(QData* data,QSoftCore::get_core()->get_project_core()->get_data_manager()->get_all_datas())
    {
        foreach(tagDataInfo *info,data->get_datas())
        {
            str=data->get_name()+"."+info->m_name;
            m_name_to_data.insert(str,info->m_uuid);
            l.append(str);
        }
    }
    ui->comboBox->addItems(l);
    ui->comboBox->setCurrentIndex(0);

    setProperty("no-ManhattanStyle",true);
}

QMemeryAddDialog::~QMemeryAddDialog()
{
    delete ui;
}

void QMemeryAddDialog::on_pushButton_2_clicked()
{
    m_property=m_name_to_property.value(ui->comboBox_2->currentText());
    m_data=m_name_to_data.value(ui->comboBox->currentText());
    close();
}

void QMemeryAddDialog::on_pushButton_clicked()
{
    close();
}

QString QMemeryAddDialog::get_property()
{
    return m_property;
}

QString QMemeryAddDialog::get_data()
{
    return m_data;
}
